package cc.fine.svc.main.controller.rel;


import cc.fine.svc.common.utils.R;
import cc.fine.svc.main.bean.FineMessage;
import cc.fine.svc.main.bean.po.RelUserFan;
import cc.fine.svc.main.bean.po.TblUser;
import cc.fine.svc.main.service.rel.RelUserFanService;
import cc.fine.svc.main.utils.MqUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wyihe
 * @since 2023-10-29
 */
@RestController
@RequestMapping("/fan")
@Api(description = "粉丝相关接口")
public class RelUserFanController {

    @Autowired
    private MqUtils mqUtils;

    @Autowired
    private RelUserFanService fanService;


    @ApiOperation("关注用户")
    @GetMapping("/{followerId}/{masterId}")
    public R follow(@PathVariable String followerId,
                    @PathVariable String masterId) {
        mqUtils.send(new FineMessage(
                FineMessage.USER_FANS_OP,
                new RelUserFan(null, masterId, followerId)
                ));

        return R.ok();
    }

    @ApiOperation("用户的关注列表, page 从0开始")
    @GetMapping("/fans/{userId}")
    public R follows(@PathVariable String userId,
                     Integer page, Integer limit) {
        List<TblUser> res = fanService.fans(userId, page, limit);
        return R.ok().data("data", res);
    }

    @ApiOperation("取消关注")
    @DeleteMapping("/{followerId}/{masterId}")
    public R disFollow(@PathVariable String followerId,
                    @PathVariable String masterId) {
        mqUtils.send(new FineMessage(
                FineMessage.Dis_User_Fans_Op,
                new RelUserFan(null, masterId, followerId)
        ));

        return R.ok();
    }

    @ApiOperation("是否关注了。返回1：已关注；2：互关；0：未关注")
    @GetMapping("/is/fan/{followerId}/{masterId}")
    public R isFan(@PathVariable String followerId,
                   @PathVariable String masterId) {
        int state = fanService.isFan(followerId, masterId);

        return R.ok().data("state", state);
    }

}

